﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using TheOutfield.UmbExt.UniversalMediaPicker.Controls;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.YouTube
{
    public class YouTubeConfigControl : AbstractConfigControl
    {
        private YouTubeConfig _config;
        private DropDownList feedTypeDropdownList = new DropDownList();
        private TextBox queryTextBox = new TextBox();
        private DropDownList orderByDropdownList = new DropDownList();
        private DropDownList safeSearchTypeDropdownList = new DropDownList();

        public override string Value
        {
            get
            {
                EnsureChildControls();

                return new YouTubeConfig
                {
                    FeedType = (YouTubeFeedType) Enum.Parse(typeof(YouTubeFeedType), feedTypeDropdownList.SelectedValue),
                    Query = queryTextBox.Text,
                    OrderBy = (YouTubeOrderBy)Enum.Parse(typeof(YouTubeOrderBy), orderByDropdownList.SelectedValue),
                    SafeSearch = (YouTubeSafeSearchType)Enum.Parse(typeof(YouTubeSafeSearchType), safeSearchTypeDropdownList.SelectedValue)
                }
                .SerializeToJson();
            }
        }

        public YouTubeConfigControl(string config)
        {
            _config = config.DeserializeJsonTo<YouTubeConfig>();
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                feedTypeDropdownList.SelectedValue = _config.FeedType.ToString();
                queryTextBox.Text = _config.Query;
                orderByDropdownList.SelectedValue = _config.OrderBy.ToString();
                safeSearchTypeDropdownList.SelectedValue = _config.SafeSearch.ToString();
            }

            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            feedTypeDropdownList.ID = "feedTypeDropdownList";
            queryTextBox.ID = "queryTextBox";
            orderByDropdownList.ID = "orderByDropdownList";
            safeSearchTypeDropdownList.ID = "safeSearchTypeDropdownList";

            feedTypeDropdownList.CssClass = "guiInputText guiInputStandardSize";
            queryTextBox.CssClass = "guiInputText guiInputStandardSize";
            orderByDropdownList.CssClass = "guiInputText guiInputStandardSize";
            safeSearchTypeDropdownList.CssClass = "guiInputText guiInputStandardSize";

            feedTypeDropdownList.DataSource = Enum.GetNames(typeof(YouTubeFeedType));
            feedTypeDropdownList.DataBind();

            orderByDropdownList.DataSource = Enum.GetNames(typeof(YouTubeOrderBy));
            orderByDropdownList.DataBind();

            safeSearchTypeDropdownList.DataSource = Enum.GetNames(typeof(YouTubeSafeSearchType));
            safeSearchTypeDropdownList.DataBind();

            Controls.Add(feedTypeDropdownList);
            Controls.Add(queryTextBox);
            Controls.Add(orderByDropdownList);
            Controls.Add(safeSearchTypeDropdownList);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddFormRow("Feed Type:", "Please select the type of feed you would like to pick content from", feedTypeDropdownList);
            writer.AddFormRow("Search Term/User ID:", "Please enter the search term, or user id to query for", queryTextBox);
            writer.AddFormRow("Order By:", "Please select how you would like you're results ordered", orderByDropdownList);
            writer.AddFormRow("Safe Search:", "Please select how restricted content should be treated", safeSearchTypeDropdownList);
        }
    }
}